package com.example.lotterysystem.service.activitystatus.operator;

import com.example.lotterysystem.dao.dataobject.ActivityUserDO;
import com.example.lotterysystem.dao.mapper.ActivityUserMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/9/13  15:38
 */
@Component
public class UserOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetStatus = convertActivityStatusDTO.getTargetUserStatus();

        try{
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }

    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetStatus = convertActivityStatusDTO.getTargetUserStatus();
        if(null == activityId
                || CollectionUtils.isEmpty(userIds)
                || null == targetStatus) {
            return false;
        }
        List<ActivityUserDO> activityUserDOList = activityUserMapper.batchSelectByAUIds(activityId,userIds);
        if(CollectionUtils.isEmpty(activityUserDOList) || activityUserDOList.size() != userIds.size()) {
            return false;
        }

        
        // 人员状态是不是为INIT
        for (ActivityUserDO auDO: activityUserDOList) {
            if(auDO.getStatus().equalsIgnoreCase(targetStatus.name())) {
                return false;
            }
        }

        return true;
    }
}
